﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace dbPoll_Application.Models
{
    public static class ReportRepository
    {
        public static int[] GetQuestionResults(int questionID)
        {
            Question question = QuestionRepository.GetQuestionByID<Question>(questionID);
            IDictionary<String, int> responses = new Dictionary<String, int>();
            IList<String> correctOptions = new List<String>();
            int[] responseStatistics = new int[2];
            var poll = PollRepository.GetPollContainingQuestion(question);

            if (question.type == QuestionType.Numeric)
            {
                // If the question is numeric
                NumericQuestion numericQuestion = QuestionRepository.GetQuestionByID<NumericQuestion>(questionID);

                int numCorrect = 0;
                int numIncorrect = 0;

                foreach (var response in numericQuestion.responses)
                {

                    // Check if the answer is correct
                    if ((Convert.ToInt32(response.response) < numericQuestion.lessThan) &&
                        (Convert.ToInt32(response.response) > numericQuestion.greaterThan))
                    {
                        numCorrect++;
                    }
                    else
                    {
                        numIncorrect++;
                    }
                }

                responses.Add("Correct [" + numericQuestion.greaterThan.ToString() +
                            " - " + numericQuestion.lessThan.ToString() + "]", numCorrect);
                responses.Add("Incorrect", numIncorrect);
                responseStatistics[0] = numCorrect + numIncorrect;
                responseStatistics[1] = numCorrect;
            }
            else if (question.type == QuestionType.Alphanumeric)
            {
                // If the question is alphanumeric
                AlphanumericQuestion alphaQuestion = QuestionRepository.GetQuestionByID<AlphanumericQuestion>(questionID);

                //  Get correct responses
                int numCorrect = 0;
                int numIncorrect = 0;
                int total = 0;

                foreach (var response in alphaQuestion.responses)
                {

                    foreach (var answer in alphaQuestion.answers)
                    {
                        if (response.response.Equals(answer.answer))
                        {
                            numCorrect++;
                        }
                    }
                    total++;
                }

                // Build a string of correct answers
                String correctAnswers = "";
                foreach (var answer in alphaQuestion.answers)
                {
                    if (correctAnswers != "")
                    {
                        correctAnswers += ", ";
                    }
                    correctAnswers += answer.answer;
                }

                responses.Add("Correct [" + correctAnswers + "]", numCorrect);
                numIncorrect = total - numCorrect;
                responses.Add("Incorrect", numIncorrect);
                responseStatistics[0] = total;
                responseStatistics[1] = numCorrect;
            }
            else
            {
                MultipleChoiceQuestion multipleQuestion = QuestionRepository.GetQuestionByID<MultipleChoiceQuestion>(questionID);
                int numCorrect = 0;
                int total = 0;

                foreach (var option in multipleQuestion.options)
                {
                    total += option.responses.Count;
                    if (option.correctCurrent)
                    {
                        numCorrect += option.responses.Count;
                    }
                }
                total = total / multipleQuestion.allowedAnswers;
                numCorrect = numCorrect / multipleQuestion.allowedAnswers;
                responseStatistics[0] = total;
                responseStatistics[1] = numCorrect;
            }
            return responseStatistics;
        }

        public static IList<int[]> GetAllQuestionResults(int pollID)
        {
            var poll = PollRepository.GetPollByID(pollID);
            var questions = poll.questions.Where(m => m.deletionTime == null);
            List<int[]> questionResults = new List<int[]>();
            foreach (var question in questions)
            {
                questionResults.Add(GetQuestionResults(question.questionID));
            }
            return questionResults;
        }
    }
}
